﻿using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using CreditAgricole.Framework;

namespace CreditAgricole.CommonElements.Event_Receivers.DocumentTemplateEventReceivers
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class DocumentTemplateEventReceivers : SPItemEventReceiver
    {
       /// <summary>
       /// An item was added.
       /// </summary>
       public override void ItemAdded(SPItemEventProperties properties)
       {
           base.ItemAdded(properties);

           bool currentFiringStatus = this.EventFiringEnabled;

           try
           {
               this.EventFiringEnabled = false;
               properties.ListItem[SPCreditAgricoleFieldId.Title] = properties.ListItem[SPBuiltInFieldId.FileLeafRef]; // Title is the filename by default
               properties.ListItem.SystemUpdate();
           }
           catch (Exception ex)
           {
               if (!string.IsNullOrEmpty(ex.Message)) CALoggingHelper.AddErrorEntry(ex.Message);
           }
           finally
           {
               this.EventFiringEnabled = currentFiringStatus;
           }
       }


    }
}
